//
//  KRReadTitleCell.m
//  cookBook
//
//  Created by kuroken on 16/1/15.
//  Copyright © 2016年 kuroken. All rights reserved.
//

#import "KRReadTitleCell.h"
#import <PureLayout.h>
#import "KRConst.h"

@interface KRCateNameBtn : UIButton
@end
@implementation KRCateNameBtn

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    self.titleLabel.x = 0;
    self.imageView.x = CGRectGetMaxX(self.titleLabel.frame) - 5;

}

@end


// cell的标识
static NSString * const reuseIdentifier = @"readTitle";

@interface KRReadTitleCell ()
@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, weak) KRCateNameBtn *cateNameBtn;
@property (nonatomic, weak) UIView *lineView;
@end

@implementation KRReadTitleCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    KRReadTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (cell == nil) {
        cell = [[KRReadTitleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        self.selectionStyle = UITableViewCellSelectionStyleNone;

        [self setupSubview];
        
        [self addConstraint];
    }
    return self;
}

- (void)setupSubview
{
    UILabel *titleLabel = [[UILabel alloc] init];
    // 标题文字大小
    titleLabel.font = [UIFont systemFontOfSize:19];
    titleLabel.numberOfLines = 0;
    [self.contentView addSubview:titleLabel];
    self.titleLabel = titleLabel;
//    titleLabel.backgroundColor = KRRandomColor;
    
    KRCateNameBtn *cateNameBtn = [[KRCateNameBtn alloc] init];
    [cateNameBtn setBackgroundImage:[UIImage imageNamed:@"btn_default_gray"] forState:UIControlStateNormal];
    [cateNameBtn setImage:[UIImage imageNamed:@"icon_arraw_gray"] forState:UIControlStateNormal];
    [cateNameBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    // 按钮文字大小
    cateNameBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:cateNameBtn];
    self.cateNameBtn = cateNameBtn;
//    cateNameBtn.backgroundColor = KRRandomColor;
    
    // 分割线
    UIView *lineView = [[UIView alloc] init];
    // 设置分隔线颜色
    lineView.backgroundColor = KRColor(188, 187, 192);
    [self.contentView addSubview:lineView];
    self.lineView = lineView;
}

- (void)addConstraint
{
    [self.titleLabel autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(20, 20, 0, 20) excludingEdge:ALEdgeBottom];
    
    [self.cateNameBtn autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:20];
    [self.cateNameBtn autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:10];
    [self.cateNameBtn autoSetDimension:ALDimensionHeight toSize:23];
    
    [self.lineView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero excludingEdge:ALEdgeTop];
    [self.lineView autoSetDimension:ALDimensionHeight toSize:1];
}

- (void)setTitle:(NSString *)title
{
    _title = title;
    [self.titleLabel setText:title];
}

- (void)setCateName:(NSString *)cateName
{
    _cateName = cateName;
    [self.cateNameBtn setTitle:[NSString stringWithFormat:@"  %@  ",cateName] forState:UIControlStateNormal];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
